<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-loading="dialogInfo.btLoading"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <page-filter
      :query="filterInfo.query"
      :filter-list="filterInfo.list"
    />
    <dialog-table
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="api"
      :listen-height="false"
      :tab-index="tabIndex"
      :query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :check-box="checkBox"
      :checkedList="checkBox ? checkedList : []"
      @handleEvent="handleEvent"
    />
  </page-dialog>
</template>

<script setup name="SelectUseStatus">
import PageDialog from "../PageDialog/index";
import PageFilter from "../PageFilter/index";
import DialogTable from "@/components/DialogTable/index";
import { useStatusPage } from "@/api/asset";

const { proxy } = getCurrentInstance();
const emit = defineEmits(["update", "callback", "reset"]);
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: true,
  },
  checkBox: {
    type: Boolean,
    default: false,
  },
  tabIndex: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: useStatusPage,
  },
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {};
    },
  },
  checkedList: {
    type: Array,
    default: function () {
      return [];
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
});

// 过滤相关配置
const filterInfo = reactive({
  query: {
    sort: "create_time desc",
    searchType: "useStatus",
    ...props.query,
  },
  list: [
    {
      type: "input",
      label: "名称",
      value: "keyword",
      event: search,
    },
    { type: "button", label: "搜索", event: search },
  ],
});
// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    { label: "状态名称", value: "fullName" },
    { label: "计提标志", value: "accrualMarkValue" },
    { label: "备注", value: "remark" },
  ],
});
// 弹窗相关
const dialogInfo = reactive({
  title: "使用状态",
  btLoading: false,
  btList: [
    { label: "关闭", event: handleClose, show: true },
    {
      label: "保存",
      type: "primary",
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
});
const selectRow = ref("");

const shows = computed({
  get() {
    return props.showDialog;
  },
  set(newValue) {
    emit("update:showDialog", newValue); // 更新父组件shows
  },
});
onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    getList();
  });
});

// 获取列表
function getList () {
  tableInfo.refresh = Math.random();
};
function search () {
  tableInfo.initCurpage = Math.random();
  tableInfo.refresh = Math.random();
};
function save () {
  handleClose();
  emit("callback", selectRow.value);
};
function handleEvent (event, data) {
  switch (event) {
    case "tableClickRow":
      selectRow.value = data;
      if (!props.saveEmpty) {
        dialogInfo.btList[1].disabled = !selectRow.value;
      }
      break;
    case "tableCheck":
      selectRow.value = data;
      if (!props.saveEmpty) {
        dialogInfo.btList[1].disabled = !selectRow.value;
      }
      break;
  }
};
// 关闭弹窗前的回调
function handleClose () {
  shows.value = false;
  emit("reset");
};
</script>

<style scoped></style>
